/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CipherModePaddingSelectorDialog.java
 *
 * Created on 08.09.2008, 14:04:07
 */
package jcrypter.utils;

import java.awt.EventQueue;
import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.swing.JFrame;
import jcrypter.JCrypterFrame;

/**
 *
 * @author uli
 */
public class CipherModePaddingSelectorDialog extends javax.swing.JDialog
{

    /** Creates new form CipherModePaddingSelectorDialog
     * @param parent
     * @param modal
     * @param ciphers
     * @param modes
     * @param paddings 
     */
    public CipherModePaddingSelectorDialog(JFrame parent, boolean modal)
    {
        super(parent, modal);
        initComponents();
        
        updateComboBoxContents();
    }

    /**
     * Re-initializes the combo box contents by reading from mainFrame. 
     */
    public void updateComboBoxContents()
    {
        Iterable<String> ciphers = JCrypterFrame.mainFrame.getCiphers();
        Iterable<String> modes = JCrypterFrame.mainFrame.getModes();
        Iterable<String> paddings = JCrypterFrame.mainFrame.getPaddings();
        //Remove combo box items
        cipherComboBox.removeAllItems();
        modeComboBox.removeAllItems();
        paddingComboBox.removeAllItems();
        //Init combo boxes
        for (String c : ciphers)
        {
            cipherComboBox.addItem(c);
        }
        for (String c : modes)
        {
            modeComboBox.addItem(c);
        }
        for (String c : paddings)
        {
            paddingComboBox.addItem(c);
        }
        //Set the selected cipher, mode and padding
        setCipher(defaultCipher);
        setMode(defaultMode);
        setPadding(defaultPadding);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cipherLabel = new javax.swing.JLabel();
        cipherComboBox = new javax.swing.JComboBox();
        modeLabel = new javax.swing.JLabel();
        modeComboBox = new javax.swing.JComboBox();
        okButton = new javax.swing.JButton();
        paddingLabel = new javax.swing.JLabel();
        paddingComboBox = new javax.swing.JComboBox();

        setTitle(i18n.getString("Select_Cipher")); // NOI18N
        setBackground(java.awt.Color.lightGray);
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("cmpDialog"); // NOI18N

        cipherLabel.setText(i18n.getString("CipherLabel.text")); // NOI18N

        cipherComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                comboBoxPropertyChange(evt);
            }
        });

        modeLabel.setText(i18n.getString("ModeLabel.text")); // NOI18N

        modeComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                comboBoxPropertyChange(evt);
            }
        });

        okButton.setText(i18n.getString("OK")); // NOI18N
        okButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonMouseClicked(evt);
            }
        });

        paddingLabel.setText(i18n.getString("PaddingLabel.text")); // NOI18N

        paddingComboBox.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                comboBoxPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(modeLabel)
                    .addComponent(paddingLabel)
                    .addComponent(cipherLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(okButton, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(paddingComboBox, 0, 98, Short.MAX_VALUE)
                    .addComponent(modeComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cipherComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 258, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cipherLabel)
                    .addComponent(cipherComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(modeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(modeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(paddingLabel)
                    .addComponent(paddingComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(okButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void okButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_okButtonMouseClicked
    this.setVisible(false);
    updateCipher();
}//GEN-LAST:event_okButtonMouseClicked

private void comboBoxPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_comboBoxPropertyChange
    changed = true;
}//GEN-LAST:event_comboBoxPropertyChange

    //Getters
    public String getCipher()
    {
        return (String) cipherComboBox.getSelectedItem();
    }

    public String getMode()
    {
        return (String) modeComboBox.getSelectedItem();
    }

    public String getPadding()
    {
        return (String) paddingComboBox.getSelectedItem() + "Padding";
    }
    //Setters
    public void setCipher(String cipher)
    {
        cipherComboBox.setSelectedItem(cipher);
    }

    public void setMode(String mode)
    {
        modeComboBox.setSelectedItem(mode);
    }

    public void setPadding(String padding)
    {
        paddingComboBox.setSelectedItem(padding);
    }

    /**
     * Updates the cipher field in the JCrypterFrame main instance
     */
    public void updateCipher()
    {
        if (!changed)
        {
            return;
        }
        //This is invoked before a OK button click is processed
        //so the cipher field is up to date at any time
        EventQueue.invokeLater(new Runnable()
        {

            @Override
            public void run()
            {
                try
                {
                    JCrypterFrame.mainFrame.setCipher(Cipher.getInstance(getCipher() +
                                                        "/" +
                                                        getMode() +
                                                        "/" +
                                                        getPadding()));
                }
                catch (NoSuchAlgorithmException ex)
                {
                    Logger.getLogger(CipherModePaddingSelectorDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
                catch (NoSuchPaddingException ex)
                {
                    Logger.getLogger(CipherModePaddingSelectorDialog.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    
    private String defaultCipher = "TWOFISH";
    private String defaultMode = "OFB";
    private String defaultPadding = "PKCS7";
    
    boolean changed; //True if parameters have changed
    //Resource bundles
    ResourceBundle i18n = ResourceBundle.getBundle("jcrypter/utils/Bundle");
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cipherComboBox;
    private javax.swing.JLabel cipherLabel;
    private javax.swing.JComboBox modeComboBox;
    private javax.swing.JLabel modeLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox paddingComboBox;
    private javax.swing.JLabel paddingLabel;
    // End of variables declaration//GEN-END:variables
}
